﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using BLL;
namespace DBLWebService
{
    /// <summary>
    /// UserMemberService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class UserMemberService : System.Web.Services.WebService
    {

        [WebMethod]
        public int DeleteUserMemberByID(int id)
        {
            return UserMemberBLL.DeleteUserMember(id);
        }

        [WebMethod]
        public DataSet GetUserMemberByID(int id)
        {
            return UserMemberBLL.GetUserMemberByID(id);
        }

        [WebMethod]
        public DataSet GetUserMemberByLoginEmail(string loginEmail)
        {
            return UserMemberBLL.GetUserMemberByLoginEmail(loginEmail);
        }

        [WebMethod]
        public DataSet GetUserMemberByDBLNo(string dblno)
        {
            return UserMemberBLL.GetUserMemberByDBLNo(dblno);
        }

        [WebMethod (Description = "这个邮箱是否能注册")]
        public bool IsLoginEmailAvaible(string loginemail)
        {
            return UserMemberBLL.IsLoginEmailAvaible(loginemail);
        }

        [WebMethod (Description = "增加用户，默认userLevelStart为当前时间，默认userLevelEnd，如果是userLevelID=0，则有效期10年，否则有效期1年")]
        public int AddUserMember(string dblNo, string loginEmail, string password,
            int userLevelID, string dblType, string userLevelStart, string userLevelEnd)
        {
            DateTime userLevelStartDateTime;
            DateTime userLevelEndDateTime;
            try
            {
                if (!(userLevelStart == null || userLevelStart.Equals("")))
                {
                    userLevelStartDateTime = Convert.ToDateTime(userLevelStart);
                }
                else
                {
                    userLevelStartDateTime = System.DateTime.Now;
                }


                if (!(userLevelEnd == null || userLevelEnd.Equals("")))
                {
                    userLevelEndDateTime = Convert.ToDateTime(userLevelEnd);
                }
                else
                {
                    if (userLevelID == 0)
                    {
                        //普通用户有效期10年
                        userLevelEndDateTime = userLevelStartDateTime.AddYears(10);
                    }
                    else//非普通用户有效期1年
                    {
                        userLevelEndDateTime = userLevelStartDateTime.AddYears(1);
                    }
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(e.Message);
            }



            return UserMemberBLL.AddUserMember(dblNo, loginEmail, password,
                userLevelID, dblType, userLevelStartDateTime, userLevelEndDateTime);
        }

        [WebMethod  (Description = "登录")]
        public string UserLogin(string loginname, string password)
        {
            return UserMemberBLL.UserLogin(loginname, password);
        }
        [WebMethod (Description="修改密码")]
        public int ChangePassword(int id, string password)
        {
            return UserMemberBLL.ChangeUserMemberPassword(id, password);
        }

        [WebMethod (Description = "更改用户等级的id和用户等级开始结束时间")]
        public int ChangeUserLevel(int id, int userLevelID, string userLevelStart, string userLevelEnd)
        {
            DateTime userLevelStartDateTime;
            DateTime userLevelEndDateTime;
            try
            {
                if (!(userLevelStart == null || userLevelStart.Equals("")))
                {
                    userLevelStartDateTime = Convert.ToDateTime(userLevelStart);
                }
                else
                {
                    userLevelStartDateTime = System.DateTime.Now;
                }


                if (!(userLevelEnd == null || userLevelEnd.Equals("")))
                {
                    userLevelEndDateTime = Convert.ToDateTime(userLevelEnd);
                }
                else
                {
                    if (userLevelID == 0)
                    {
                        //普通用户有效期10年
                        userLevelEndDateTime = userLevelStartDateTime.AddYears(10);
                    }
                    else//非普通用户有效期1年
                    {
                        userLevelEndDateTime = userLevelStartDateTime.AddYears(1);
                    }
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(e.Message);
            }

            return UserMemberBLL.ChangeUserMemberLevel(id, userLevelID, userLevelStartDateTime, userLevelEndDateTime);
        }
    }
}
